﻿using Microsoft.Office.Interop.Excel;
using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using SUNRPA.CONTROLS.IMPL.util;
using System.Collections.Concurrent;
using ExcelRange = Microsoft.Office.Interop.Excel.Range; 

namespace SUNRPA.CONTROLS.IMPL.controls {
    /// <summary>
    /// </summary>
    public class ReadExcelNAction:BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            ReadExcelNParam? param = item.ControlParam as ReadExcelNParam;
            string excelObject = param.ExcelObject;
            Workbook wb = this.GetObjVar<ExcelModel>(ref excelObject, globleData).WorkBookValue as Workbook;
            if(wb == null) { 
                throw new RpaRuntimeException(String.Format("Excel对象{0}不存在", excelObject));
            }

            ExcelUtil excelUtil = new ExcelUtil();
            excelUtil.SetApp(wb);

            int readRange = param.ReadRange;

            string startRow = param.StartRow;
            string endRow = param.EndRow;
            string startColumn = param.StartColumn;
            string endColumn = param.EndColumn;
            this.GetStringVar(ref startRow, globleData);
            this.GetStringVar(ref endRow, globleData);
            this.GetStringVar(ref startColumn, globleData);
            this.GetStringVar(ref endColumn, globleData);
            
            string sheetName = param.SheetName;
            this.GetStringVar(ref sheetName, globleData);

            int startRowIndex = excelUtil.GetRowNum(startRow, sheetName);
            int startColumnIndex = excelUtil.GetColumnNum(startColumn, sheetName);
            int endRowIndex = excelUtil.GetRowNum(endRow, sheetName);
            int endColumnIndex = excelUtil.GetColumnNum(endColumn, sheetName);


            string excelDataOut = param.ExcelDataOut;
            this.GetStringVar(ref excelDataOut, globleData);
            ExcelRange range;

            switch(readRange){ 
                case 0:
                    //单元格
                    range = excelUtil.GetRange(sheetName, startRowIndex, startColumnIndex);
                    break;
                case 1:
                    //行
                    int lastColumn = excelUtil.GetColumnNum("-1", sheetName);
                    range = excelUtil.GetRange(sheetName,startRowIndex, 1, startRowIndex, lastColumn);
                    break;
                case 2:
                    //列
                    int lastRow = excelUtil.GetRowNum("-1", sheetName);
                    range = excelUtil.GetRange(sheetName,1, startColumnIndex, lastRow, startColumnIndex);
                    break;
                case 3:
                    //区域
                    range = excelUtil.GetRange(sheetName,startRowIndex, startColumnIndex, endRowIndex, endColumnIndex);
                    break;
                case 4:
                    //已使用区域
                    range = excelUtil.GetRange(sheetName);
                    break;
                default:
                    range = excelUtil.GetRange(sheetName);
                    break;
            }
            object value = range.get_Value();
            bool readShowContent = param.ReadShowsContent;
            bool trimContent = param.TrimContent;

            if(readRange == 0) {
                //单元格为单值
                if(readShowContent) { 
                    value = range.Text;
                    if(trimContent && value !=null) {
                        value = value.ToString().Trim();
                    }
                }
            } else { 
                //区域为二维object数组
                if(readShowContent) { 
                    object[,] values = value as object[,];
                    object[,] tempValue = new object[values.GetLength(0), values.GetLength(1)];
                    for(int i = 0; i < values.GetLength(0); i++) {
                        for(int j = 0; j < values.GetLength(1); j++) {
                            object text = (range.Cells[i + 1,j + 1] as ExcelRange).Text;
                            if(trimContent && text !=null) { 
                                text = text.ToString().Trim();
                            }
                            tempValue[i,j] = text;
                        }
                    }
                    value = tempValue;
                }
            }

            this.saveVar(globleData, excelDataOut, new OtherModel(excelDataOut, value), DefaultContents.RUNNING_PARAM);
            return item.EndTag.Index;
        }
    }
}
